<?php
class TuserController extends AppController {

	var $name = 'Tuser';
	var $components = array('Xacl');
	var $uses = Array('Tmenu','Tconf', 'Tuser','Tusergrouprelation', 'Tactiongrouprelation','Tmenugrouprelation');

	/**
	 * welcome page
	 *
	 */
	function index() {

		$this->layout = 'tuser';
	}

	/**
     * login and do login action
     * @return
     */

	function login($url = NULL) {
		$url = empty($url) ? '/tuser/index' : base64_decode($url);
		if ($this->Xacl->check_login()) {
			$this->flash("已经登录", $url);
			return;
		}
		if (! empty($_POST)) {
			$_user = isset($_POST['u']) ? $_POST['u'] : NULL;
			$_psw = isset($_POST['p']) ? $_POST['p'] : NULL;

			if ($this->Tuser->checkUserPsw($_user, $_psw)) {			
				/**
				 * 刷新缓存
				 */
				$config_data = Cache::read('sysytemconfig');
				if(empty($config_data)){
					$confVar = $this->Tconf->find('all', array('fields'=>array('f_key','f_value','f_isstring')));
				    if(!empty($confVar)){
					    $sysconfig = array();
					    foreach($confVar as $conf)
					    {
						    $key = $conf['Tconf']['f_key'];
						    $value = stripslashes($conf['Tconf']['f_value']);
						    if($conf['Tconf']['f_isstring'] == 0){
							    $value = @unserialize($value);
						    }
						    $sysconfig[$key] = $value;
					    }
					    Cache::write('sysytemconfig',$sysconfig);			 
				    }
				}
				$this->Xacl->write_login($_user,$url);
			} else {
				$this->flash("登录失败", "/tuser/login");
				return;
			}
		}
		$this->set('refmsg',$this->Xacl->Session->read('refmsg'));
		$this->set("ref",$url);
		$this->layout = NULL;
	}
	/**
     * user list
     * @return
     */
	function ulist() {

		$this->paginate = array('fields'=>array('Tuser.id', 'Tuser.f_uname', 'Tuser.f_status'), 'limit'=>10, 'order'=>'Tuser.f_uname desc');

		$this->loadModel("Tuser");

		$this->set('userlist', $this->paginate($this->Tuser));

		$this->layout = 'tuser';
	}
	/**
	 * create new user form
	 *
	 */
	function newuser() {
		$this->layout = 'tuser';
	}
	/**
	 * create new user action
	 *
	 */
	function newuserdo() {
		if (! empty($_POST)) {
			$data = array('f_uname'=>isset($_POST["fname"]) ? trim($_POST["fname"]) : '', 'f_psw'=>isset($_POST["fpsw"]) ? trim($_POST["fpsw"]) : '', );

			if ( empty($data['f_uname'])) {
				$this->flash("用户名不能为空!", "/tuser/newuser");
				return;
			}

			if ( empty($data['f_psw'])) {
				$this->flash("用户密码不能为空!", "/tuser/newuser");
				return;
			}
			if (false != $this->Tuser->findByUserName($data['f_uname'])) {
				$this->flash("用户名被占用", "/tuser/newuser");
				return;
			}
			if ($this->Tuser->createUser($data['f_uname'], $data['f_psw'])) {
				$this->flash("添加成功!", "/tuser/ulist");
				return;
			} else {
				$this->flash("添加失败!", "/tuser/newuser");
				return;
			}
		}
		$this->flash("用户名不能为空!", "/tuser/newuser");
		return;
	}
	/**
     * stop user
     * @param int $id [optional]
     * @return
     */
	function stopuser($id = NULL) {
		if ( empty($id)) {
			$this->flash("无效操作!", "/tuser/ulist");
			return;
		}
		$status = $this->Tuser->field('f_status', "id='$id'");
		if (1 == $status) {
			$this->Tuser->save(array('id'=>$id, 'f_status'=>2));
			$this->flash("停用设置成功!", "/tuser/ulist");
			return;
		} else {
			$this->Tuser->save(array('id'=>$id, 'f_status'=>1));
			$this->flash("启用设置成功!", "/tuser/ulist");
			return;
		}
	}
	/**
	 * change user password form
	 *
	 * @param int $id user id
	 * @return
	 */
	function chpsw($id = NULL) {
		if (NULL == $id) {
			echo '操作无效!';
			return '';
		}

		$this->set('uid', $id);
		$this->layout = NULL;
	}
	/**
	 * change user password action
	 *
	 * @return unknown
	 */
	function chpswdo() {
		$id = isset($_POST['uid']) ? $_POST['uid'] : NULL;
		if (NULL == $id) {
			echo '操作无效!';
			return '';
		}
		$psw1 = isset($_POST['psw1']) ? $_POST['psw1'] : NULL;
		$psw2 = isset($_POST['psw2']) ? $_POST['psw2'] : NULL;
		if ( empty($psw1)) {
			$this->flash('失败，密码不能为空!', '/tuser/chpsw/'.$id);
			return '';
		}
		if ($psw1 != $psw2) {
			$this->flash('失败，2次密码不相同!', '/tuser/chpsw/'.$id);
			return '';
		}
		$data = array('id'=>$id, 'f_psw' => md5($psw2));
		$this->Tuser->save($data);
		$this->flash('操作成功!', '/tuser/chpsw/'.$id);

		return '';
	}

	/**
	 * delete user
	 *
	 * @param int $uid user id
	 * @return
	 */
	function deluser($uid=NULL)
	{
		if (NULL == $uid) {
			echo '操作无效!';
			return '';
		}
		$this->Tuser->deleteAll("id='$uid'");
		$this->Tmenugrouprelation->deleteAll("f_uid='$uid'");
		$this->Tactiongrouprelation->deleteAll("f_uid='$uid'");
		$this->Tusergrouprelation->deleteAll("f_uid='$uid'");

		$this->flash('操作成功!', '/tuser/ulist/');
		return '';
	}
	/**
     * logout action
     * @return
     */
	function out() {


		$this->Xacl->logout();

	}
	/**
     * not allow msg
     * @return
     */
	function reest() {
		$this->layout = null;
	}


}
?>
